import { isUndef } from '@/afterSales/utils/common'
import { conditionE } from '@/afterSales/components/conditionFilter/filterData'
// eslint-disable-next-line no-restricted-imports
import { difference, intersection } from 'lodash'
const strInclude = (filterValue, value) => value && filterValue.some(item => value.includes(item))
const strExclusive = (filterValue, value) => !value || !filterValue.some(item => value.includes(item))
const is = (filterValue, value) => {
  return filterValue === value
}
const isNot = (filterValue, value) => filterValue !== value
const prefix = (filterValue, value) => value && value.startsWith(filterValue)
const suffix = (filterValue, value) => value && value.endsWith(filterValue)
const isNull = (filterValue, value) => {
  if (value instanceof Array) {
    return value.length === 0
  } else {
    return !value
  }
}
const isNotNull = (filterValue, value) => {
  if (value instanceof Array) {
    return value.length !== 0
  } else {
    return value
  }
}
const inScope = (max, min, value) => value && (value >= min && value <= max)
const notInScope = (max, min, value) => value && (value < min || value > max)
const less = (filterValue, value) => value && (value < filterValue)
const greater = (filterValue, value) => value && (value > filterValue)
const greaterThan = (filterValue, value) => value && (value >= filterValue)
const lessThan = (filterValue, value) => value && (value <= filterValue)
const commonCondition = {
  include: strInclude,
  exclusive: strExclusive,
  is,
  isNot,
  prefix,
  suffix,
  isNull,
  isNotNull,
  inScope,
  notInScope,
  equal: is,
  unequal: isNot,
  greater,
  less,
  greaterThan,
  lessThan,
  before: less,
  past: greater
}
const cascadeInclude = (filterValue = [], value) => {
  return filterValue.every((item, index) => {
    return item && item === value[index]
  })
}
const cascadeExclusive = (filterValue, value) => {
  return !cascadeInclude(filterValue, value)
}
// eslint-disable-next-line no-void
const loop = () => void 0
const arrInclude = (filterValue, value) => intersection(filterValue, value).length && value.length !== 0
const arrExclusive = (filterValue, value) => {
  return intersection(filterValue, value).length === 0 || value.length === 0
}
const cascadeConditon = {
  include: cascadeInclude,
  exclusive: cascadeExclusive
}
const includeType = ['include', 'exclusive']
const arrIs = (filterValue, value) => difference(filterValue, value).length === 0 && difference(value, filterValue).length === 0
const arrNot = (filterValue, value) => {
  return difference(filterValue, value).length !== 0 || difference(value, filterValue).length !== 0
}
function split (data, type = ',') {
  return data.split(type)
}
const multiConditon = {
  include: arrInclude,
  exclusive: arrExclusive,
  is: arrIs,
  isNot: arrNot,
  isNull,
  isNotNull
}
const scopedType = ['inScope', 'notInScope']
export function conditonFn (conditions: any[] = [], form) {
  for (const one of conditions) {
    if (isUndef(form[one.businessId])) { // 判定是null 或是undefined
      // 为空 不为空判断
      const type = conditionE[one.filterType]
      if (type === 'isNotNull' || type === 'isNull') {
        const fn = commonCondition[type]
        return fn(null, null)
      }
    }
    if (!conditionProcess(one, form[one.businessId])) {
      return false
    }
  }
  return true
}
export function conditionProcess (condition, value) {
  // 筛选类型
  const type = conditionE[condition.filterType]
  if (condition.fieldType === 'checkbox') { // 多选
    const fn = multiConditon[type] || loop
    const filterValue = condition.filterValue ? split(condition.filterValue) : []
    return fn(filterValue, (value || []).map(item => item.toString()))
  } else if (condition.fieldType === 'region' || condition.fieldType === 'cascadeSelect') {
    const fn = (includeType.includes(type) ? cascadeConditon[type] : multiConditon[type]) || loop
    return fn(condition.filterValue || [], value || [])
  } else if (condition.fieldType === 'department' || condition.fieldType === 'member') {
    // 部门，成员
    const fn = multiConditon[type] || loop
    const filterValue = condition.filterValue ? split(condition.filterValue) : []
    value = value ? split(value) : []
    return fn(filterValue, value)
  } else {
    const fn = commonCondition[type] || loop
    if (scopedType.includes(type)) {
      // 在范围内，不在范围内
      return fn(condition.maxFilterValue, condition.minFilterValue, value)
    } else if (includeType.includes(type)) {
      // 包含。不包含
      // text类型的分隔符为$^$
      const splitType = condition.fieldType === 'text' ? '&^&' : ','
      const filterValue = condition.filterValue ? split(condition.filterValue, splitType) : []
      return fn(filterValue, (value || '').toString())
    } else if (condition.fieldType === 'number') {
      const filterValue = condition.filterValue
      return fn(filterValue, value ? Number(value) : value)
    } else {
      const filterValue = condition.filterValue
      return fn(filterValue, (value || '').toString())
    }
  }
  return true
}
export function conditonJudgment (conditionArr = [], form) {
  if (!conditionArr.length) {
    return true
  }
  return conditionArr.some(conditions => {
    return conditonFn(conditions, form)
  })
}
